package com.example.hotel_lxy.ui.activity;

import android.content.Intent;
import android.os.Bundle;
import android.text.method.HideReturnsTransformationMethod;
import android.text.method.PasswordTransformationMethod;
import android.util.Log;
import android.view.View;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.recyclerview.widget.LinearLayoutManager;

import com.alibaba.android.arouter.facade.annotation.Route;
import com.alibaba.android.arouter.launcher.ARouter;
import com.bumptech.glide.Glide;
import com.example.hotel_lxy.R;
import com.example.hotel_lxy.application.ARouterPath;
import com.example.hotel_lxy.bean.BeanBean;
import com.example.hotel_lxy.bean.HelpCenterBean;
import com.example.hotel_lxy.net.Api;
import com.example.hotel_lxy.utils.SPUtils;
import com.ysxsoft.common_base.base.BaseActivity;
import com.ysxsoft.common_base.base.RBaseAdapter;
import com.ysxsoft.common_base.base.RViewHolder;
import com.ysxsoft.common_base.utils.JsonUtils;
import com.ysxsoft.common_base.utils.ToastUtils;
import com.zhy.http.okhttp.OkHttpUtils;
import com.zhy.http.okhttp.callback.StringCallback;

import butterknife.BindView;
import butterknife.ButterKnife;
import okhttp3.Call;

/**
 * 修改密码
 */
@Route(path = "/main/ChangePasswordActivity")
public class ChangePasswordActivity extends BaseActivity implements View.OnClickListener {
    @BindView(R.id.back)
    ImageView back;
    @BindView(R.id.img_primarypassword)
    ImageView imgPrimarypassword;
    @BindView(R.id.img_newpassword)
    ImageView imgNewpassword;
    @BindView(R.id.img_confirmpassword)
    ImageView imgConfirmpassword;

    @BindView(R.id.editText_primarypassword)
    EditText editTextPrimarypassword;
    @BindView(R.id.editText_newpassword)
    EditText editTextNewpassword;
    @BindView(R.id.text_confirmpassword)
    EditText textConfirmpassword;
    @BindView(R.id.text_submit)
    TextView textSubmit;
    private boolean aBoolean = true;
    private boolean bBoolean = true;
    private boolean cBoolean = true;

    public static void start() {
        ARouter.getInstance().build(ARouterPath.getChangePasswordActivity())
                .navigation();
    }


    @Override
    protected int getLayoutId() {
        return R.layout.activity_change_password;
    }

    @Override
    public void doWork() {
        super.doWork();
        back.setOnClickListener(this);
        textSubmit.setOnClickListener(this);
        imgPrimarypassword.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if (aBoolean) {
                    aBoolean = false;
                    Glide.with(ChangePasswordActivity.this).load(R.mipmap.icon_so).into(imgPrimarypassword);
                    editTextPrimarypassword.setTransformationMethod(HideReturnsTransformationMethod.getInstance());//显示
                } else {
                    aBoolean = true;
                    Glide.with(ChangePasswordActivity.this).load(R.mipmap.icon_invisible).into(imgPrimarypassword);
                    editTextPrimarypassword.setTransformationMethod(PasswordTransformationMethod.getInstance());//隐藏
                }
            }
        });
        imgNewpassword.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if (bBoolean) {
                    bBoolean = false;
                    Glide.with(ChangePasswordActivity.this).load(R.mipmap.icon_so).into(imgNewpassword);
                    editTextNewpassword.setTransformationMethod(HideReturnsTransformationMethod.getInstance());//显示
                } else {
                    bBoolean = true;
                    Glide.with(ChangePasswordActivity.this).load(R.mipmap.icon_invisible).into(imgNewpassword);
                    editTextNewpassword.setTransformationMethod(PasswordTransformationMethod.getInstance());//隐藏
                }
            }
        });
        imgConfirmpassword.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if (cBoolean) {
                    cBoolean = false;
                    Glide.with(ChangePasswordActivity.this).load(R.mipmap.icon_so).into(imgConfirmpassword);
                    textConfirmpassword.setTransformationMethod(HideReturnsTransformationMethod.getInstance());//显示
                } else {
                    cBoolean = true;
                    Glide.with(ChangePasswordActivity.this).load(R.mipmap.icon_invisible).into(imgConfirmpassword);
                    textConfirmpassword.setTransformationMethod(PasswordTransformationMethod.getInstance());//隐藏
                }
            }
        });

    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.back:
                finish();
                break;
            case R.id.text_submit:
                showDataSubmit();
                break;
        }
    }

    private void showDataSubmit() {
        if(editTextPrimarypassword.getText().toString().equals("")){
            ToastUtils.shortToast(ChangePasswordActivity.this,"原密码不能为空");
            return;
        }
        if(editTextNewpassword.getText().toString().equals("")){
            ToastUtils.shortToast(ChangePasswordActivity.this,"新密码不能为空");
            return;
        }
        if(!textConfirmpassword.getText().toString().equals(editTextNewpassword.getText().toString())){
            ToastUtils.shortToast(ChangePasswordActivity.this,"两次密码不一样");
            return;
        }

        String token = SPUtils.get(ChangePasswordActivity.this, "token", "").toString();
        showLoadingDialog("请求中");
        OkHttpUtils.post()
                .url(Api.POST_SETPWD)
                .addHeader("flag", token)
                .addParams("old_password", editTextPrimarypassword.getText().toString())
                .addParams("new_password", editTextNewpassword.getText().toString())
                .addParams("again_password", textConfirmpassword.getText().toString())
                .tag(this)
                .build()
                .execute(new StringCallback() {
                    @Override
                    public void onError(Call call, Exception e, int id) {
                        Log.e("TAG", "修改密码Exception~~~~~~~~    " + e.getMessage());
                        hideLoadingDialog();
                    }

                    @Override
                    public void onResponse(String response, int id) {
                        Log.e("TAG", "修改密码onResponse~~~~~~~~    " + response);
                        hideLoadingDialog();
                        BeanBean helpCenterBean = JsonUtils.parseByGson(response, BeanBean.class);
                        BeanBean beanBean= JsonUtils.parseByGson(response, BeanBean.class);
                        if(beanBean.getCode().equals("4")){
                            SPUtils.put(ChangePasswordActivity.this,"token","");
                            Intent intent = new Intent(ChangePasswordActivity.this, LoginActivity.class).
                                    setFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK | Intent.FLAG_ACTIVITY_NEW_TASK);
                            startActivity(intent);
                            return;
                        }
                        if (beanBean.getCode().equals("0")) {
                            ToastUtils.shortToast(ChangePasswordActivity.this,helpCenterBean.getMsg());
                            finish();
                        } else{
                            ToastUtils.shortToast(ChangePasswordActivity.this,helpCenterBean.getMsg());
                        }
                    }
                });
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        // TODO: add setContentView(...) invocation
        ButterKnife.bind(this);
    }
}